﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SharePointEditor.BusinessDefinitions.Interfaces;
using SharePointEditor.BusinessDefinitions.Classes;

namespace SPContextWorker
{
    public class Gateway : ISPDataService
    {
        private IWebApplication _webApplication;
        public IWebApplication WebApplication
        {
            get { return _webApplication ?? (_webApplication = GetWebApplication()); } set{_webApplication = value;}
        }

        public IWebApplication GetWebApplication()
        {
            if (SPContext.Current.Site != null)
            {
                SPWebApplication spWebApplication = SPContext.Current.Site.WebApplication;
                return new SPEWebApplication
                           {
                               Title = spWebApplication.Name,
                               Url = spWebApplication.GetResponseUri(SPUrlZone.Default).AbsoluteUri,
                               SiteCollections = GetSiteCollectionsInWebApplication(spWebApplication)
                           };
            }
            throw new ApplicationException("No SPContext, using incorrect Gateway!");
        }

        public IEnumerable<SPESiteCollection> GetSiteCollectionsInWebApplication(SPWebApplication spWebApplication)
        {
            var siteCollections = new List<SPESiteCollection>();
            for (var i = 0; i < spWebApplication.Sites.Count; i++)
            {
                var rootWeb = spWebApplication.Sites[i].RootWeb;
                siteCollections.Add(new SPESiteCollection
                                        {
                                            Title = rootWeb.Title,
                                            Url = rootWeb.Url,
                                            Sites = GetSubSitesInSPWeb(rootWeb)
                                        });
            }
            return siteCollections;
        }

        public IEnumerable<SPESite> GetSubSitesInSPWeb(SPWeb spWeb)
        {
            var sites = new List<SPESite>();
            foreach (SPWeb subweb in spWeb.Webs)
            {
                sites.Add(new SPESite
                              {
                                  Title = subweb.Name,
                                  Url = subweb.Url,
                                  Lists = GetListsInSPWeb(subweb)
                              });
            }
            return sites;
        }

        public IEnumerable<SPEList> GetListsInSPWeb(SPWeb web)
        {
            var lists = new List<SPEList>();
            foreach (SPList list in web.Lists)
            {
                var splist = new SPEList
                                 {
                                     Title = list.Title,
                                     Url = list.DefaultViewUrl,
                                     Items = GetItemsInSPList(list)
                                 };
                lists.Add(splist);
            }
            return lists;
        }

        public IEnumerable<SPEListItem> GetItemsInSPList(SPList list)
        {
            var sharePointListItems = new List<SPEListItem>();
            if (list.Items.Count > 0)
            {
                foreach (SPListItem spListItem in list.Items)
                {
                    sharePointListItems.Add(new SPEListItem{Title = spListItem.Name});
                }
            }
            return sharePointListItems;
        }

        public SPWeb GetSPWebFromSiteName(string siteName)
        {
            //TODO could be improved?
            for (var i = 0; i < SPContext.Current.Site.WebApplication.Sites.Count; i++)
            {
                if (SPContext.Current.Site.WebApplication.Sites[i].RootWeb.Title.Replace(" ", "").ToLower() == siteName.Replace(" ", "").ToLower())
                {
                    return SPContext.Current.Site.WebApplication.Sites[i].RootWeb;
                }
                if (SPContext.Current.Site.WebApplication.Sites[i].RootWeb.Webs.Any(x => x.Title.Replace(" ", "").ToLower()  == siteName.Replace(" ", "").ToLower()))
                {
                    return SPContext.Current.Site.WebApplication.Sites[i].RootWeb.Webs.FirstOrDefault(
                            x => x.Title.Replace(" ", "").ToLower() == siteName.Replace(" ", "").ToLower());

                }
            }
            throw new ApplicationException("The site "+siteName+" does not exist in this web application");
        }

        public SPList GetSPListFromSPWeb(SPWeb web, string listTitle)
        {
            foreach (var list in web.Lists.Cast<SPList>().Where(list => list.Title == listTitle))
            {
                return list;
            }
            throw new ApplicationException("The list " + listTitle + " does not exist in the site " + web.Name);
        }
    }
}
